﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BCCommon;
using BCDto.Sim.Admin.AdminPermissionConfig;
using BCDto.Common.TreeNode;
using BCService.Sim.Admin.AdminPermissionConfig;
using BCService.Utils;
using BCTMSRESTful.Configs;

namespace BCTMSRESTful.Controllers.Management.Admin.AdminPermission
{
    /// <summary>
    /// 员工权限
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class AdminPermissionConfigController : ManagementBaseController
    {
        private readonly IAdminPermissionConfigService adminPermissionConfigService;

        public AdminPermissionConfigController(IAdminPermissionConfigService adminPermissionConfigService)
        {
            this.adminPermissionConfigService = adminPermissionConfigService;
        }

        #region 创建员工权限
        /// <summary>
        /// 创建员工权限
        /// </summary>
        /// <param name="requestDto">员工权限</param>
        /// <response code="201">创建员工权限信息</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加员工权限")]
        [ProducesResponseType(typeof(AdminPermissionConfigDto), 200)]
        public IActionResult Post([FromBody] AdminPermissionConfigRequestDto requestDto)
        {
            var result = this.adminPermissionConfigService.Add(requestDto);
            return Created(string.Format("/api/v1/sim/AdminPermissionConfig/{0}", result.AdminPermissionConfigId), result);
        }
        #endregion

        #region 修改员工权限
        /// <summary>
        /// 修改员工权限
        /// </summary>
        /// <param name="adminPermissionConfigId">员工权限Id</param>
        /// <param name="requestDto">员工权限</param>
        /// <response code="201">创建员工权限信息</response>
        /// <returns></returns>
        [HttpPut("{admin_permission_config_id}")]
        [Log("修改员工权限")]
        [ProducesResponseType(typeof(AdminPermissionConfigDto), 200)]
        public IActionResult Put([FromRoute(Name = "admin_permission_config_id")]long adminPermissionConfigId,[FromBody] AdminPermissionConfigRequestDto requestDto)
        {
            AdminPermissionConfigPutDto putDto = requestDto.As<AdminPermissionConfigPutDto>();
            putDto.AdminPermissionConfigId = adminPermissionConfigId;
            return Ok(this.adminPermissionConfigService.Update(putDto));
        }
        #endregion

        #region 删除员工权限
        /// <summary>
        /// 删除员工权限
        /// </summary>
        /// <param name="adminPermissionConfigId">员工权限Id</param>
        /// <returns></returns>
        /// <response code="204">刪除一条员工权限成功。</response>
        [HttpDelete("{admin_permission_config_id}")]
        [Log("删除员工权限")]
        public IActionResult Delete([FromRoute(Name = "admin_permission_config_id")]long adminPermissionConfigId)
        {
            this.adminPermissionConfigService.Delete(adminPermissionConfigId);
            return NoContent();
        }
        #endregion

        #region 获取员工权限接口（分页）
        /// <summary>
        /// 获取员工权限接口（分页）
        /// </summary>
        /// <param name="permissionName">权限名称</param>
        /// <param name="permissionCode">权限码</param>
        /// <param name="permissionPathCode">权限路径码</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">升序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">页面数据条数</param>
        /// <response code="200">获取员工权限接口（分页）。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<AdminPermissionConfigDto>), 200)]
        public IActionResult Query([FromQuery(Name = "permission_name")]string permissionName,
            [FromQuery(Name = "permission_code")]string permissionCode,
            [FromQuery(Name = "permission_path_code")]string permissionPathCode,
            [FromQuery(Name = "sort_name")]string sortName,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            var list = this.adminPermissionConfigService.Query(permissionName,permissionCode,permissionPathCode, sortName, ascending, index, size);
            return this.Ok(list);
        }
        #endregion

        #region 获取员工权限列表
        /// <summary>
        /// 获取员工权限列表
        /// </summary>
        /// <response code="200">获取员工权限列表。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(IEnumerable<AdminPermissionConfigDto>), 200)]
        public IActionResult Get()
        {
            var list = this.adminPermissionConfigService.GetList();
            return this.Ok(list);
        }
        #endregion

        #region 获取未使用的员工权限列表
        /// <summary>
        /// 获取未使用的员工权限列表
        /// </summary>
        /// <response code="200">获取未使用的员工权限列表。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<TreeNodeDto<AdminPermissionConfigDto>>), 200)]
        public IActionResult Unused()
        {
            var list = this.adminPermissionConfigService.GetUnusedList();
            return this.Ok(list);
        }
        #endregion

        #region 修复权限码
        /// <summary>
        /// 修复权限码
        /// </summary>
        /// <response code="204">修复权限码成功</response>
        /// <returns></returns>
        [HttpPut("[action]")]
        [Log("修复权限码")]
        public IActionResult FixErrorPermissionCode()
        {
            this.adminPermissionConfigService.FixErrorPermissionConfigRecord();
            return NoContent();
        }
        
        #endregion
    }
}
